<?php
/*
 * Created on 08/07/2014 by lion
 *
 */
class DBFHelper{
	
	public function readDBF($filepath,$key_array=null,$condition=null){
		$dbf = new dbf_class($filepath);
	    $num_rec=$dbf->dbf_num_rec;
	    $field_num=$dbf->dbf_num_field;
	    
		$array_1 = array();
	    for($i=0; $i<$num_rec; $i++){
	        if ($row = $dbf->getRow($i)) {
	        	$array_2  = array();
		        for($j=0; $j<$field_num; $j++){
		        	for($m=0;$m<count($key_array);$m++){
		        		if($key_array[$m]==$dbf->dbf_names[$j]['name']){
		        			if($dbf->dbf_names[$j]['type']=='F'){
		        				$array_2[$j] =$this->NumToStr($row[$j]);
		        			}
		        			else{
		        				$array_2[$j] = $row[$j];		
		        			}
		        		}
		        	}
	    	    }
	    	    $array_1[$i] = $array_2;
	        }
	    }
	    
	    return $array_1;
	    
	}
	
	
	private function NumToStr($num){
	    if (stripos($num,'e')===false) return $num;
	    $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');
	    $result = "";
	    $flag  = false;
	    if($num<0){
	    	$num =  abs($num);
	    	$flag = true;
	    }
	    while ($num > 0){
	        $v = $num - floor($num / 10)*10;
	        $num = floor($num / 10);
	        $result   =   $v . $result;
	    }
	    
	    if($flag){
	    	$result = "-".$result;
	    }
	    return $result;
	}
	
}
?>
